package com.kly.cashmall.popup;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.kly.cashmall.R;
import com.kly.cashmall.module.profile.adapter.GenericSimpleAdapter;
import com.kly.cashmall.widget.GetText;

import java.util.ArrayList;
import java.util.List;

import razerdp.basepopup.BasePopupWindow;

/**
 * @author zxp
 * @since 27/10/21
 */
public class SelectListPopup extends BasePopupWindow {

    public SelectListPopup(@NonNull Context context) {
        super(context);
        setContentView(R.layout.dialog_common_list_view);
    }

    public SelectListPopup setTitle(String title) {
        TextView textDialogTitle = findViewById(R.id.list_dialog_title);
        textDialogTitle.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(title)) {
            textDialogTitle.setText(title);
        } else {
            textDialogTitle.setText("");
        }
        return this;
    }

    public <T> SelectListPopup setList(final List<T> list, GetText<T> getText, int selectedIndex,
                                       final AdapterView.OnItemClickListener onItemClickListener) {
        GenericSimpleAdapter<T> simpleTextAdapter =
                new GenericSimpleAdapter<>(getContext(), new ArrayList<T>(), selectedIndex);

        simpleTextAdapter.setGetText(getText);

        ListView listView = findViewById(R.id.list_dialog_list_view);

        listView.setAdapter(simpleTextAdapter);

        simpleTextAdapter.clear();
        simpleTextAdapter.addAll(list);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(parent, view, position, id);
                }
                dismiss();
            }
        });

        return this;
    }
}
